package com.example.tcp.client.store.impl;

import com.bluejeans.common.bigqueue.BigQueue;
import com.example.tcp.client.RetryTcpClient;
import com.example.tcp.client.serde.Serde;
import com.example.tcp.client.store.AbstractDequeStore;

import java.io.IOException;

public class BigQueueFileQueueDequeStore<T> extends AbstractDequeStore<T> {

    private BigQueue queue;
    private Serde<T> serde;

    public BigQueueFileQueueDequeStore(String dir, String name, Serde<T> serde) {
        this.queue = new BigQueue(dir, name);
        this.serde = serde;
    }

    @Override
    protected boolean _enqueue(T e) {
        this.queue.enqueue(serde.serialize(e));
        return true;
    }

    @Override
    protected T _dequeue() {
        return serde.deserialize(queue.dequeue());
    }

    @Override
    protected void _close() {
        try {
            queue.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isEmpty() {
        return queue.isEmpty();
    }

    @Override
    public Object wrap() {
        return queue;
    }


    @Override
    public T peek() {
        return serde.deserialize(queue.peek());
    }

    @Override
    public long size() {
        return queue.size();
    }
}
